/*
 * 版权所有 ( c ) 联想集团有限公司 2005-2009 保留所有权利。
 * 项目：	Lenovo Push Service
 * 文件名：	IoTest.java
 * 版本:    1.0
 * 描述：	
 * 作者：	刘兵召
 * 日期：	May 7, 2010 
 * 修改历史：
 * 【时间】		【修改者】	【修改内容】
 */
package com.feng.demo.jakartacommons.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.io.FileSystemUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;

import com.feng.util.ImgUtil;

public class IoTest {

	/**
	 * @param args
	 * @throws IOException
	 * @throws MalformedURLException
	 */
	public static void main(String[] args) throws MalformedURLException,
			IOException {
		// TODO Auto-generated method stub
		/*ioUtilsUrlTest();
		ioUtilsUrlTest_WithIOUtils();
		fileUtilTest();
		fileUtilTest();
		filenameUtilsTest();
		fileSystemUtilsTest();
		lineIteratorTest();*/
		fileListFromDir();
	}

	/**
	 * IOUtils contains utility methods dealing with reading, writing and
	 * copying. The methods work on InputStream, OutputStream, Reader and
	 * Writer. As an example, consider the task of reading bytes from a URL, and
	 * printing them. This would typically done like this:
	 */
	public static void ioUtilsUrlTest() throws MalformedURLException,
			IOException {
		InputStream in = new URL("http://10.100.121.46/ams/").openStream();
		try {
			InputStreamReader inR = new InputStreamReader(in,"utf-8");
			BufferedReader buf = new BufferedReader(inR);
			String line;
			while ((line = buf.readLine()) != null) {
				System.out.println(line);
			}
		} finally {
			in.close();
		}
	}

	/**
	 * the function is the same to method above:ioUtilsUrlTest() With the
	 * IOUtils class, that could be done with:
	 */
	public static void ioUtilsUrlTest_WithIOUtils()
			throws MalformedURLException, IOException {
		InputStream in = new URL("http://dl.lenovomm.com/ams/fileman/img/snap/s_2010-04-15011600-12713085608764821.png").openStream();
		try {
			System.out.println(IOUtils.toString(in,"utf-8"));
		} finally {
			IOUtils.closeQuietly(in);
		}
	}
	
	/**
	 *  The FileUtils  class contains utility methods for working with File objects. These include reading, writing, copying and comparing files.
		For example to read an entire file line by line you could use: 
	 * @throws IOException 
	 */
	public static void fileUtilTest() throws IOException{
		File file = new File("E:/code/MyTest/src/com/feng/demo/jakartacommons/io/test.properties");
		 List lines = FileUtils.readLines(file, "UTF-8");
		 for(Object  str : lines){
			 System.out.println(str.toString());
		 }
	}
	/**
	 *  The FilenameUtils  class contains utility methods for working with filenames without  using File objects. The class aims to be consistent between Unix and Windows, to aid transitions between these environments (such as moving from development to production).
	 *  For example to normalize a filename removing double dot segments: 
	 */
	public static void filenameUtilsTest(){
		String filename = "C:/commons/io/../lang/project.xml";
		 String normalized = FilenameUtils.normalize(filename);
		 // result is "C:/commons/lang/project.xml"
		 System.out.println("File name is :"+filename);
		 System.out.println("File name after nomalized is :"+normalized);
	}
	/**
	 * The FileSystemUtils class contains utility methods for working with the file system to access functionality not supported by the JDK. Currently, the only method is to get the free space on a drive. Note that this uses the command line, not native code. 
	 * For example to find the free space on a drive: 
	 * @throws IOException 
	 */
	public static void fileSystemUtilsTest() throws IOException{
		 long freeSpace = FileSystemUtils.freeSpace("C:/");
		 System.out.println("C:/ free space is:"+freeSpace/1024/1024/1024+"G");
	}
	
	/**
	 * The org.apache.commons.io.LineIterator class provides a flexible way for working with a line-based file. An instance can be created directly, or via factory methods on FileUtils or IOUtils. The recommended usage pattern is: 
	 * @throws IOException 
	 */
	public static void lineIteratorTest() throws IOException{
		File file = new File("E:/code/MyTest/src/com/feng/demo/jakartacommons/io/test.properties");
		 LineIterator it = FileUtils.lineIterator(file,"UTF-8");
		 try {
		   while (it.hasNext()) {
		     String line = it.nextLine();
		     /// do something with line
		     System.out.println(line);
		   }
		 } finally {
		   LineIterator.closeQuietly(it);
		 }
	}
	
	/**
	 * 从指定目录中读取指定后缀的图片来压缩
	 * @throws FileNotFoundException
	 */
	public static void fileListFromDir() throws FileNotFoundException{
		String [] ext = {"png"};
		Collection collection = FileUtils.listFiles(new File("D:/img/"), ext, true);
		Iterator iter = collection.iterator();
		while(iter.hasNext()){
			File file = (File)iter.next();
			File fileOut = new File("D:/img/s_"+file.getName());
			InputStream inputStream = new FileInputStream(file);
			OutputStream outputStream = new FileOutputStream(fileOut);
			ImgUtil.zipImageFile(inputStream,outputStream,210,350,0.6f);
		}
	}
}

